//
//  NewsViewController.m
//  Movie
//
//  Created by mycomputer on 15/7/22.
//  Copyright (c) 2015年 杨韦智. All rights reserved.
//

#import "NewsViewController.h"
#import "UIViewExt.h"
#import "UIImageView+WebCache.h"
#import "DataService.h"
#import "NewsModel.h"
#import "Base.h"
#import "NewsTableViewCell.h"
#import "NewsImageListViewController.h"

@interface NewsViewController ()
{
    UIImageView *_headerImageView;
    UILabel *_headerLabel;
}
@end

@implementation NewsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"新闻";
    }
    return  self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createNewsTableView];
    [self _createNavHeaderView];
    [self _loadData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - loadData
- (void)_loadData {
    NSArray *dataArray = [DataService getJsonDataFromFile:@"news_list.json"];
    _newsModelArray = [[NSMutableArray alloc] init];
    for (NSDictionary *dic in dataArray) {
        NewsModel *model = [[NewsModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [_newsModelArray addObject:model];
    }
    NewsModel *model = _newsModelArray[0];
    NSURL *url = [NSURL URLWithString:model.image];
    [_headerImageView sd_setImageWithURL:url];
    _headerLabel.text = model.title;
    [_newsModelArray removeObject:_newsModelArray[0]];
}

#pragma mark - createSubViewControllers


- (void)_createNavHeaderView {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 200)];
    headView.backgroundColor = [UIColor clearColor];
    _newsTableView.tableHeaderView = headView;
    
    _headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kNavHeight, kWidth, 200)];
    [self.view addSubview:_headerImageView];
    
    _headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kWidth, 30)];
    _headerLabel.backgroundColor = [UIColor lightGrayColor];
    _headerLabel.textColor = [UIColor whiteColor];
    _headerLabel.bottom = _headerImageView.bottom;
    
    [self.view addSubview:_headerLabel];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offY = _newsTableView.contentOffset.y;
    
    if (offY > -64) {
        _headerImageView.top = -offY;
    }else {
        CGFloat newHeight = ABS(offY)-64+200;
        CGFloat newWidth = kWidth/200*newHeight;
        _headerImageView.frame = CGRectMake((kWidth-newWidth)/2, kNavHeight, newWidth, newHeight);
    }
    _headerLabel.bottom = _headerImageView.bottom;
}


- (void)_createNewsTableView {
    _newsTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _newsTableView.delegate = self;
    _newsTableView.dataSource = self;
    [self.view addSubview:_newsTableView];
    UINib *nib = [UINib nibWithNibName:@"NewsTableViewCell" bundle:[NSBundle mainBundle]];
    [_newsTableView registerNib:nib forCellReuseIdentifier:@"newsCell"];
    
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _newsModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newsCell" forIndexPath:indexPath];
    NewsModel *model = _newsModelArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsImageListViewController *imageListViewController = [[NewsImageListViewController alloc] init];
    [self.navigationController pushViewController:imageListViewController animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
